package org.arch_learn.io.bio;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class SocketClient {
    public static void main(String[] args) throws IOException {
        //1.创建Socket对象
        Socket socket = new Socket("127.0.0.1", 9999);
        //2.从连接中输出消息
        OutputStream outputStream = socket.getOutputStream();
        System.out.println("请输入请求内容：");
        Scanner scanner = new Scanner(System.in);
        String nextLine = scanner.nextLine();
        outputStream.write(nextLine.getBytes());
        //3.从连接中取出服务端回复
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int read = inputStream.read(bytes);
        System.out.println("服务端响应为：" + new String(bytes, 0, read));
        //4.关闭连接
        socket.close();
    }
}
